/*
 * @Author: 许警 11378748333@
 * @Date: 2024-08-12 09:12:28
 * @LastEditors: 许警 11378748333@
 * @LastEditTime: 2024-09-11 17:28:46
 * @FilePath: /hsxlsystem/src/store/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStore } from 'vuex'

import user from './modules/user'
import { getLoginInfo } from '@/api/login/index'
import { getToken, setToken, removeToken } from '@/utils/app'
import { useRouter } from 'vue-router' //路由
import permission from './modules/permission'
import { clear } from '@/utils/app'
const router = useRouter()

export default createStore({
  state: {
    token: '' || '', //用户token
    // user: localStorage.getItem('user') || '', //用户个人信息
    userId: localStorage.getItem('userId') || '',
    userInfo: localStorage.getItem('user') || '',
    roles: [], //用户角色,
    keepAliveList: [], //页面缓存列表、后期面包屑用到、现在只针对单独页面
    routers: localStorage.getItem('routers') || [],
    newAsyncRoutes: [], // 异步路由表
    account: localStorage.getItem('account'), // 登录成功后的用户账号
    menus: [] //
  },
  getters: {
    roles: state => state.roles
  },
  mutations: {
    setToken(state, payload) {
      setToken(payload)
      state.token = payload
    },

    setUserId(state, payload) {
      localStorage.setItem('userId', payload)
      state.userId = payload
    },
    setUser(state, payload) {
      localStorage.setItem('user', payload)
      state.userInfo = payload
      console.log('用户1', state.userInfo)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    setRouters: (state, payload) => {
      localStorage.setItem('routers', payload)
      // state.routers = [...payload]
    },
    setNewAsyncRoutes(state, payload) {
      state.newAsyncRoutes = payload
    },
    resetNewAsyncRoutes(state) {
      state.newAsyncRoutes = []
    },
    setAccount(state, payload) {
      state.account = payload
      localStorage.setItem('account', payload)
    },
    setMenus(state, payload) {
      state.menus = clear(payload)
    }
  },
  actions: {
    setLoginInfo({ commit, state }, payload) {
      // console.log('用户token是------',payload.token)
      commit('setToken', payload.token)
      commit('setUserId', payload.user.id)
      commit('setUser', JSON.stringify(payload.user))
      // commit('setUser', payload.user)
      commit('SET_ROLES', ['admin']) //如果是管理员，测试下按钮级别的路由
      commit('setRouters', JSON.stringify(payload.name))
    },
    // 退出登录
    logOut({ commit, state }, payload) {
      removeToken()
      localStorage.clear() //清除本地缓存()
    },
    // 更新缓存列表
    updatekeepAliveList({ commit, state }, payload) {
      state.keepAliveList.push(payload)
    },

    // 全局 挂载方法没用，只能在这里写了
    // 删除对象中为空的属性
    deleteObjBlank({ commit }, payload) {
      for (const key in payload) {
        if (!payload[key]) {
          delete payload[key]
        }
      }
      return payload
    }
    // 判断对象是否为空
  },
  modules: { user, permission }
})
